<template>
  <div class="page">
  <el-form
    class="personal-info"
    :model="info">
    <el-form-item label="账号/工号" prop="account">
      <el-input v-model="info.account" disabled/>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="info.name" :disabled="!edit"/>
    </el-form-item>
  </el-form>
  <el-button v-if="!edit" @click="handleEdit">编辑</el-button>
  <div v-else>
    <el-button  @click="handleUpdate">保存</el-button>
    <el-button @click="handleCancel">取消</el-button>
  </div>
  </div>
</template>

<script>

export default {
  name: "PersonInfo",
  props:['personInfo','callBack','reloadStaff'],
  data(){
    return{
      edit:false,
      info:this.personInfo,
      buffer:{},
    }
  },
  mounted() {
    //this.info=this.personInfo;
    //console.log(JSON.stringify(this.info));
    //console.log(JSON.stringify(this.personInfo))
  },
  methods:{
    handleEdit(){
      this.buffer=JSON.stringify(this.personInfo);
      this.edit=true;
    },
    handleCancel(){
      this.info=JSON.parse(this.buffer);
      this.buffer=null;
      this.edit=false;
    },
    handleUpdate(){
      this.callBack(this.info).then(resp=>{
        this.$message({
          type:'success',
          message:'信息更新成功',
        })
        this.edit=false;
        console.log(resp.data);
        this.$store.commit('staff/login',resp.data);
        this.reloadStaff(this.$store.state.staff);
      })
    }
  }
}
</script>

<style scoped>
  .personal-info{
    width: 60vw;
  }
  .page{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>
